package com.lst.projectlib.base;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lst.projectlib.R;
import com.lst.projectlib.dialog.CustomProgressDialog;

public abstract class BaseFram extends Fragment {
	protected CustomProgressDialog mDialog = null;
	protected Context mContext = null;
	protected Handler mHandler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			refresh(msg.what, msg.obj);
			return false;
		}
	});

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		mContext = getActivity();
		if (mDialog == null) {
			mDialog = new CustomProgressDialog(mContext, R.style.progress_dialog);
		}

		View view = initView(inflater, container, savedInstanceState);
		initData();
		return view;
	};
	
	protected abstract View initView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState);

	protected abstract void initData();

	public void runLoadThread(final int messageId, final Object params) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message msg = new Message();
				msg.what = messageId;

				// Load data for the activity.
				msg.obj = loadData(messageId, params);
				mHandler.sendMessage(msg);
			}
		}).start();
	}
	
	protected void refresh(int messageId, Object params) {}

	protected Object loadData(int messageId, Object params) {
		return null;
	}


	@Override
	public void onDestroy() {
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.dismiss();
		}
		super.onDestroy();
	}
}
